﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class BillingDailyInvDA : IBillingDailyInvDA
    {
        private string CREATED_BY = "@CREATED_BY";
        private string DAYS = "@DAYS";
        private string IS_STATEMENT = "@IS_STATEMENT";
        private string IS_STORAGE_CHARGES = "@IS_STORAGE_CHARGES";
        private string OWNER_ID = "@OWNER_ID";
        private string SQL_CHECK_BILLING_DAILY_INV_ID_UNIQUE = " SELECT COUNT(1) FROM @_@BILLING_DAILY_INV WHERE DAYS = @DAYS AND OWNER_ID = @OWNER_ID AND IS_STORAGE_CHARGES = @IS_STORAGE_CHARGES ";
        private string SQL_DELETE_BILLING_DAILY_INV = " DELETE FROM @_@BILLING_DAILY_INV WHERE DAYS = @DAYS AND OWNER_ID = @OWNER_ID AND IS_STORAGE_CHARGES = @IS_STORAGE_CHARGES ";
        private string SQL_INSERT_BILLING_DAILY_INV = " INSERT INTO @_@BILLING_DAILY_INV ( WH_ID, DAYS, OWNER_ID, IS_STATEMENT, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE,IS_STORAGE_CHARGES) VALUES ( @WH_ID, @DAYS, @OWNER_ID, @IS_STATEMENT, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE(),@IS_STORAGE_CHARGES )  ";
        private string SQL_SELECT_ALL_BILLING_DAILY_INV = " SELECT  ROWID, WH_ID, DAYS, OWNER_ID, IS_STATEMENT, TS, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE,IS_STORAGE_CHARGES  FROM  @_@BILLING_DAILY_INV ";
        private string SQL_SELECT_ALL_BILLING_DAILY_INV_INIT_DATE = " SELECT DISTINCT CONVERT(VARCHAR(20),I.DAYS,23)  AS DAYS,CASE ISNULL(BDI.DAYS,'') WHEN '' THEN 'N' ELSE 'Y' END AS IS_BILLING_CREDENCE, CASE ISNULL(BDI.IS_STATEMENT,'') WHEN '' THEN 'N' ELSE BDI.IS_STATEMENT END AS  IS_STATEMENT, @OWNER_ID AS OWNER_ID FROM SYSADM.INIT_DATE I  LEFT JOIN @_@BILLING_DAILY_INV BDI ON BDI.DAYS = CONVERT(VARCHAR(20),I.DAYS,23) AND BDI.OWNER_ID = @OWNER_ID AND BDI.IS_STORAGE_CHARGES = 'N'  WHERE EXISTS (SELECT OWNER_ID FROM @_@OWNER WHERE OWNER_ID = @OWNER_ID)  AND I.DAYS  <= (GETDATE()-1) ";
        private string SQL_SELECT_ALL_BILLING_DAILY_INV_STORAGE_CHARGES = " SELECT DISTINCT O.OWNER_ID,CASE ISNULL(BDI.DAYS,'') WHEN ''  THEN @DAYS ELSE  BDI.DAYS END AS DAYS,CASE ISNULL(BDI.DAYS,'') WHEN '' THEN 'N' ELSE 'Y' END AS IS_BILLING_CREDENCE,  CASE ISNULL(BDI.IS_STATEMENT,'') WHEN '' THEN 'N' ELSE BDI.IS_STATEMENT END AS IS_STATEMENT   FROM @_@OWNER O  LEFT JOIN @_@BILLING_DAILY_INV BDI ON O.OWNER_ID = BDI.OWNER_ID AND BDI.IS_STORAGE_CHARGES = 'Y' AND BDI.DAYS = @DAYS  INNER JOIN @_@CONTRACT C ON C.OWNER_ID = O.OWNER_ID AND C.IS_AUDIT = 'Y'   INNER JOIN SYSADM.USER_OWNER UO ON UO.OWNER_ID = O.OWNER_ID AND UO.USER_ID = @USER_ID ";
        private string SQL_SELECT_ALL_DAILY_INV_DETAIL = " DECLARE @DAYS datetime SET @DAYS = @CURRENT_DAYS SELECT CONVERT(VARCHAR(20),@DAYS,23) AS DOC_ID,'' AS EXTERNAL_DOC_ID,OWNER_ID,'Owner' AS BILLING_OBJECT,OWNER_ID AS BILLING_OBJECT_ID,BILLING_OBJECT_NAME,'InvCharges' AS BILLING_TYPE,SKU_ID,SUM(BEGINQTY) AS BEGINQTY,SUM(INQTY) AS INQTY,SUM(OTQTY) AS OTQTY, SUM(AJQTY) AS AJQTY,SUM(QTY) AS QTY,  (SELECT  ISNULL(SUM(ISNULL(CASEQTY,0)),0) AS CASEQTY FROM ( SELECT CASE DOC_TYPE WHEN 'SO' THEN -SUM(ISNULL([CASE],0)) ELSE SUM(ISNULL([CASE],0)) END AS CASEQTY FROM @_@INV_PALLET WHERE OWNER_ID = TEMP.OWNER_ID AND SKU_ID = TEMP.SKU_ID AND EXEC_DATE < @DAYS+1 GROUP BY DOC_TYPE ) TEMPCASE) CASEQTY,  (SELECT  ISNULL(SUM(ISNULL(PALLETQTY,0)),0) AS CASEQTY FROM ( SELECT CASE DOC_TYPE WHEN 'SO' THEN -SUM(ISNULL([BILLING_PALLET],0)) ELSE SUM(ISNULL([PALLET],0)) END AS PALLETQTY FROM @_@INV_PALLET WHERE OWNER_ID = TEMP.OWNER_ID AND SKU_ID = TEMP.SKU_ID AND EXEC_DATE < @DAYS+1 GROUP BY DOC_TYPE ) TEMPPALLET) PALLETQTY   FROM (   SELECT A.OWNER_ID,OWNER.COMPANY AS BILLING_OBJECT_NAME, A.SKU_ID, s.DESCR, s.DESCR2, s.SKU_CLASS_ID, s.[CUBE],   s.GROSS_WGT, u.UOM_DESCR,s.PACK_ID, la.VENDOR_ID,  SUM(A.BEGINQTY) AS BEGINQTY, SUM(A.INQTY) AS INQTY, SUM(A.OTQTY) AS OTQTY,   SUM(A.AJQTY) AS AJQTY,  ISNULL(SUM(A.ENDQTY),0) AS QTY,ISNULL(SUM(A.ENDQTY),0) * s.[CUBE] AS TOTALCUBE,   ISNULL(SUM(A.ENDQTY),0) * s.GROSS_WGT AS TOTALWGT   FROM (  SELECT IT.LOT, IT.OWNER_ID, IT.SKU_ID,   SUM(CASE WHEN IT.TRANS_DATE < @DAYS AND IT.SOURCE_TRANS_TYPE NOT IN ('Picking','PutawayMove','ConsoleMove') THEN IT.QTY ELSE 0 END) AS BEGINQTY ,  SUM(CASE WHEN IT.TRANS_DATE < (@DAYS + 1) AND IT.SOURCE_TRANS_TYPE NOT IN ('Picking','PutawayMove','ConsoleMove') THEN IT.QTY ELSE 0 END) AS ENDQTY,  SUM(CASE WHEN IT.TRANS_DATE >= @DAYS AND IT.TRANS_DATE < (@DAYS + 1)  AND IT.TRANS_TYPE + IT.SOURCE_TRANS_TYPE + IT.[STATUS] = 'INRECEIPTDETAILADDOK' THEN IT.QTY ELSE 0 END) AS INQTY,  SUM(CASE WHEN IT.TRANS_DATE >= @DAYS AND IT.TRANS_DATE < (@DAYS + 1)  AND IT.TRANS_TYPE + IT.SOURCE_TRANS_TYPE   IN ('AJADJUSTMENTDETAILADD','AJCYCLECOUNTADD','MVTRANSFERDETAILADD','MVTRANSFERDETAILUPDATE') THEN IT.QTY ELSE 0 END) AS AJQTY,  SUM(CASE WHEN IT.TRANS_DATE >= @DAYS AND IT.TRANS_DATE < (@DAYS + 1) AND IT.TRANS_TYPE+IT.STATUS = 'OTOK' THEN IT.QTY ELSE 0 END) AS OTQTY   FROM @_@INV_TRANS IT   GROUP BY IT.LOT, IT.OWNER_ID, IT.SKU_ID) A  LEFT JOIN @_@INV_LOT il ON il.LOT = A.LOT   LEFT JOIN @_@SKU s ON s.OWNER_ID = A.OWNER_ID AND s.SKU_ID = A.SKU_ID  LEFT JOIN @_@PACK p ON p.PACK_ID = s.PACK_ID   LEFT JOIN @_@UOM u ON u.UOM_ID = p.PACK_UOM1   LEFT JOIN @_@LOT_ATTRIBUTE la ON la.LOT = A.LOT   LEFT JOIN @_@OWNER OWNER ON OWNER.OWNER_ID = A.OWNER_ID  WHERE (ISNULL(A.BEGINQTY,0) <> 0 OR ISNULL(A.INQTY,0) <> 0 OR ISNULL(A.AJQTY,0) <> 0 OR ISNULL(A.OTQTY,0) <> 0 OR ISNULL(A.ENDQTY,0) <> 0) AND A.OWNER_ID = @OWNER_ID   GROUP BY A.OWNER_ID,OWNER.COMPANY, A.SKU_ID, s.DESCR, s.DESCR2, s.SKU_CLASS_ID, s.[CUBE], s.GROSS_WGT,u.UOM_DESCR, la.VENDOR_ID,s.PACK_ID) [TEMP]  GROUP BY OWNER_ID,SKU_ID,BILLING_OBJECT_NAME ";
        private string SQL_UPDATE_BILLING_DAILY_INV = " UPDATE @_@BILLING_DAILY_INV SET WH_ID = @WH_ID, IS_STATEMENT = @IS_STATEMENT, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE DAYS = @DAYS AND OWNER_ID = @OWNER_ID AND IS_STORAGE_CHARGES = @IS_STORAGE_CHARGES  ";
        private string TS = "@TS";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";

        public bool CheckBillingDailyInvIDUnique(string billingDailyInvID, string ownerID, string isStorageCharges, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@DAYS", billingDailyInvID), new SqlParameter("@OWNER_ID", ownerID), new SqlParameter("@IS_STORAGE_CHARGES", isStorageCharges) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_BILLING_DAILY_INV_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckBillingDailyInvIDUnique(DataBase dataBase, DbTransaction tran, string billingDailyInvID, string ownerID, string isStorageCharges, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@DAYS", billingDailyInvID), new SqlParameter("@OWNER_ID", ownerID), new SqlParameter("@IS_STORAGE_CHARGES", isStorageCharges) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_BILLING_DAILY_INV_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteBillingDailyInv(List<string> billingDailyInvID, List<string> ownerID, string isStorageCharges, List<string> whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < billingDailyInvID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@DAYS", billingDailyInvID[j]), new SqlParameter("@OWNER_ID", ownerID[j]), new SqlParameter("@IS_STORAGE_CHARGES", isStorageCharges) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_BILLING_DAILY_INV.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int DeleteBillingDailyInv(DataBase dataBase, DbTransaction tran, List<string> billingDailyInvID, List<string> ownerID, string isStorageCharges, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < billingDailyInvID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@DAYS", billingDailyInvID[j]), new SqlParameter("@OWNER_ID", ownerID[j]), new SqlParameter("@IS_STORAGE_CHARGES", isStorageCharges) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_BILLING_DAILY_INV.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllBillingDailyInv(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_BILLING_DAILY_INV.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllBillingDailyInv(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_BILLING_DAILY_INV.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public BillingDailyInvInfo GetBillingDailyInvByID(string billingdailyinvID, string ownerID, string isStorageCharges, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_BILLING_DAILY_INV.Replace("@_@", whLoginID) + " WHERE DAYS = @DAYS AND OWNER_ID = @OWNER_ID AND IS_STORAGE_CHARGES = @IS_STORAGE_CHARGES  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@DAYS", billingdailyinvID), new SqlParameter("@OWNER_ID", ownerID), new SqlParameter("@IS_STORAGE_CHARGES", isStorageCharges) };
            BillingDailyInvInfo billingDailyInvInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    billingDailyInvInfo = new BillingDailyInvInfo((reader["ROWID"].ToString() != "") ? decimal.Parse(reader["ROWID"].ToString()) : 0M, reader["WH_ID"].ToString(), reader["DAYS"].ToString(), reader["OWNER_ID"].ToString(), reader["IS_STATEMENT"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime(), reader["IS_STORAGE_CHARGES"].ToString());
                }
            }
            return billingDailyInvInfo;
        }

        public BillingDailyInvInfo GetBillingDailyInvByID(DataBase dataBase, DbTransaction tran, string billingdailyinvID, string ownerID, string isStorageCharges, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_BILLING_DAILY_INV.Replace("@_@", whLoginID) + " WHERE DAYS = @DAYS AND OWNER_ID = @OWNER_ID  AND IS_STORAGE_CHARGES = @IS_STORAGE_CHARGES  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@DAYS", billingdailyinvID), new SqlParameter("@OWNER_ID", ownerID), new SqlParameter("@IS_STORAGE_CHARGES", isStorageCharges) };
            BillingDailyInvInfo billingDailyInvInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                billingDailyInvInfo = new BillingDailyInvInfo((reader["ROWID"].ToString() != "") ? decimal.Parse(reader["ROWID"].ToString()) : 0M, reader["WH_ID"].ToString(), reader["DAYS"].ToString(), reader["OWNER_ID"].ToString(), reader["IS_STATEMENT"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime(), reader["IS_STORAGE_CHARGES"].ToString());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return billingDailyInvInfo;
        }

        public DataSet GetBillingDailyInvByQueryList(List<string> sqlWhere, BillingDailyInvQueryEntity billingdailyinvQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_BILLING_DAILY_INV.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!billingdailyinvQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, billingdailyinvQuery.CurrentPage, billingdailyinvQuery.PageSize, billingdailyinvQuery.SortField, billingdailyinvQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetBillingDailyInvByQueryList(List<string> sqlWhere, BillingDailyInvQueryEntity billingDailyInvQueryEntity, string whLoginID, PubParasInfo pubParasInfo)
        {
            string temp = this.SQL_SELECT_ALL_BILLING_DAILY_INV_INIT_DATE.Replace("@_@", whLoginID).Replace("@OWNER_ID", "'" + billingDailyInvQueryEntity.OwnerID + "'");
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                temp = temp + " AND " + sqlWhere[i].ToString();
            }
            if (!billingDailyInvQueryEntity.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, billingDailyInvQueryEntity.CurrentPage, billingDailyInvQueryEntity.PageSize, billingDailyInvQueryEntity.SortField, billingDailyInvQueryEntity.SortDirection).Replace("@LANGUAGE", "''" + pubParasInfo.UserLanguage + "''");
            }
            else
            {
                temp = temp.Replace("@LANGUAGE", "'" + pubParasInfo.UserLanguage + "'");
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetBillingDailyInvByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, BillingDailyInvQueryEntity billingdailyinvQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_BILLING_DAILY_INV.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            temp = PagingHelper.GetPagingSQL(temp, billingdailyinvQuery.CurrentPage, billingdailyinvQuery.PageSize, billingdailyinvQuery.SortField, billingdailyinvQuery.SortDirection);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public List<BillingOrderDetailInfo> GetBillingDailyInvDetailByOwnerIDAndDays(string ownerID, string days, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_DAILY_INV_DETAIL.Replace("@_@", whLoginID).Replace("@CURRENT_DAYS", "'" + days + "'").Replace("@OWNER_ID", "'" + ownerID + "'");
            List<BillingOrderDetailInfo> billingOrderDetailListInfo = new List<BillingOrderDetailInfo>();
            BillingOrderDetailInfo billingOrderDetailInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql))
            {
                while (reader.Read())
                {
                    billingOrderDetailInfo = new BillingOrderDetailInfo(reader["DOC_ID"].ToString(), reader["EXTERNAL_DOC_ID"].ToString(), reader["OWNER_ID"].ToString(), reader["BILLING_OBJECT"].ToString(), reader["BILLING_OBJECT_ID"].ToString(), reader["BILLING_OBJECT_NAME"].ToString(), reader["BILLING_TYPE"].ToString(), reader["SKU_ID"].ToString(), decimal.Parse((reader["QTY"].ToString() == "") ? "0" : reader["QTY"].ToString()), decimal.Parse((reader["CASEQTY"].ToString() == "") ? "0" : reader["CASEQTY"].ToString()), decimal.Parse((reader["PALLETQTY"].ToString() == "") ? "0" : reader["PALLETQTY"].ToString()));
                    billingOrderDetailListInfo.Add(billingOrderDetailInfo);
                }
            }
            return billingOrderDetailListInfo;
        }

        public DataSet GetBillingDailyInvStorageChargesByQueryList(List<string> sqlWhere, BillingDailyInvQueryEntity billingDailyInvQueryEntity, string whLoginID, PubParasInfo pubParasInfo)
        {
            string temp = this.SQL_SELECT_ALL_BILLING_DAILY_INV_STORAGE_CHARGES.Replace("@_@", whLoginID).Replace("@DAYS", "'" + billingDailyInvQueryEntity.Days + "'");
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!billingDailyInvQueryEntity.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, billingDailyInvQueryEntity.CurrentPage, billingDailyInvQueryEntity.PageSize, billingDailyInvQueryEntity.SortField, billingDailyInvQueryEntity.SortDirection).Replace("@LANGUAGE", "''" + pubParasInfo.UserLanguage + "''").Replace("@USER_ID", "''" + pubParasInfo.UserID + "''");
            }
            else
            {
                temp = temp.Replace("@LANGUAGE", "'" + pubParasInfo.UserLanguage + "'").Replace("@USER_ID", "''" + pubParasInfo.UserID + "''");
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public int InsertBillingDailyInv(BillingDailyInvInfo billingDailyInvInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    billingDailyInvInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_BillingDailyInv_Parameters(billingDailyInvInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_BILLING_DAILY_INV.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int InsertBillingDailyInv(DataBase dataBase, DbTransaction tran, BillingDailyInvInfo billingDailyInvInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    billingDailyInvInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_BillingDailyInv_Parameters(billingDailyInvInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_BILLING_DAILY_INV.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_BillingDailyInv_Parameters(BillingDailyInvInfo billingDailyInvInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 50), new SqlParameter(this.DAYS, SqlDbType.VarChar, 20), new SqlParameter(this.OWNER_ID, SqlDbType.VarChar, 30), new SqlParameter(this.IS_STATEMENT, SqlDbType.VarChar, 1), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 30), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 30), new SqlParameter(this.IS_STORAGE_CHARGES, SqlDbType.VarChar, 1) };
            if (!string.IsNullOrEmpty(billingDailyInvInfo.WhID))
            {
                paramArray[0].Value = billingDailyInvInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = billingDailyInvInfo.Days;
            paramArray[2].Value = billingDailyInvInfo.OwnerID;
            if (!string.IsNullOrEmpty(billingDailyInvInfo.IsStatement))
            {
                paramArray[3].Value = billingDailyInvInfo.IsStatement;
            }
            else
            {
                paramArray[3].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(billingDailyInvInfo.CreatedBy))
            {
                paramArray[4].Value = billingDailyInvInfo.CreatedBy;
            }
            else
            {
                paramArray[4].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(billingDailyInvInfo.UpdatedBy))
            {
                paramArray[5].Value = billingDailyInvInfo.UpdatedBy;
            }
            else
            {
                paramArray[5].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(billingDailyInvInfo.IsStorageCharges))
            {
                paramArray[6].Value = billingDailyInvInfo.IsStorageCharges;
                return paramArray;
            }
            paramArray[6].Value = DBNull.Value;
            return paramArray;
        }

        public int UpdateBillingDailyInv(BillingDailyInvInfo billingDailyInvInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    billingDailyInvInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_BillingDailyInv_Parameters(billingDailyInvInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_BILLING_DAILY_INV.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int UpdateBillingDailyInv(DataBase dataBase, DbTransaction tran, BillingDailyInvInfo billingDailyInvInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    billingDailyInvInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_BillingDailyInv_Parameters(billingDailyInvInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_BILLING_DAILY_INV.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

